package com.ten.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.AclUser;
import com.ten.security.utils.OssUtils;
import com.ten.security.utils.TokenToAll;
import com.ten.system.dao.AclUserDao;
import com.ten.system.service.AclUserService;
import com.ten.common.core.vo.Result;
import com.ten.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * @author ：Q大侠
 * @date ：Created in 2022/12/1 19:54
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class AclUserServiceImpl implements AclUserService {

    @Autowired(required = false)
    private AclUserDao aclUserDao;

    @Override
    public Result findByName(String username) {
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("is_deleted",0);
        AclUser aclUser = aclUserDao.selectOne(wrapper);
        if (aclUser!=null){
            return new Result<AclUser>(2000,"查询信息成功",aclUser);
        }
        return null;
    }
    /** 
    * @create by:  Q大侠
    * @description:  获取当前登录的用户对象
    * @create time: 2022/12/4 21:00
    * @param
    * @return  Result<AclUser>
    */
    @Override
    public Result<AclUser> findUserinfo() {
        String username = TokenToAll.ToName();
        QueryWrapper<AclUser> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("is_deleted",0);
        AclUser aclUser = aclUserDao.selectOne(wrapper);
        return new Result<>(2000,"获取用户信息",aclUser);
    }

    /** 
     * @create by:  Q大侠
     * @description: //如果使用mp分页 需要配置分页拦截器 
     * @create time: 2022/12/4 21:52
      * @param current,pageSize,userVo 
     * @return Result<IPage<AclUser>>
     */
    @Override
    public Result<IPage<AclUser>> findByConditionPage(Integer current, Integer pageSize, UserVo userVo) {
        IPage<AclUser> page=new Page(current,pageSize);

        QueryWrapper<AclUser> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(userVo.getUsername())){
            wrapper.like("username",userVo.getUsername());
        }
        if(StringUtils.hasText(userVo.getStartDate())){
            wrapper.ge("gmt_create",userVo.getStartDate());
        }
        if(StringUtils.hasText(userVo.getEndDate())){
            wrapper.le("gmt_create",userVo.getEndDate());
        }
        IPage<AclUser> iPage = aclUserDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询成功",iPage);
    }

    @Override
    public Result<IPage<AclUser>> updateUserStatusByid(String id, Integer isDeleted) {
        int i = aclUserDao.updateUserStatusByid(id, isDeleted);
        String massage = null;
        if (isDeleted==1){
            massage = "禁用";
        }else {
            massage = "启用";
        }
        if (i==1){
            return new Result<>(2000,massage+"成功!");
        }return new Result<>(4000,massage+"失败!");
    }

    @Override
    public Result<IPage<AclUser>> addUserByUser(AclUser aclUser) {
        //判断用户名是否已存在
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",aclUser.getUsername());
        if (aclUserDao.selectOne(wrapper)!=null){
            return new Result<>(4000,"用户名已存在,添加失败!");
        }
        //生成id--首位为1+随机生成十八位随机数
        String sb = "1";
        for (int i = 0; i < 18; i++) {
            sb=sb+(int)(Math.random() * 10);
        }
        //生成默认密码--123456
        PasswordEncoder ps = new BCryptPasswordEncoder();
        String encode = ps.encode("123456");
        //完善需要添加的对象
        aclUser.setId(sb);
        aclUser.setPassword(encode);
        aclUser.setIsDeleted(0);
        aclUser.setAvator("https://makabaka4399.oss-cn-hangzhou.aliyuncs.com/vue/70aadded-640c-408a-98cf-decf68f2facd.jpg");
        aclUser.setGmtCreate(LocalDateTime.now());
        aclUser.setGmtModified(LocalDateTime.now());
        int insert = aclUserDao.insert(aclUser);
        if (insert ==1){
            return new Result<>(2000,"添加成功!");
        }
        return new Result<>(4000,"添加失败!");
    }

    @Override
    public Result<IPage<AclUser>> deleteUserById(String id) {
        int i = aclUserDao.deleteById(id);
        if (i ==1){
            return new Result<>(2000,"删除成功!");
        }
        return new Result<>(4000,"删除失败!");
    }

    @Override
    public Result<IPage<AclUser>> updateUserByUser(AclUser aclUser) {
        int i = aclUserDao.updateUserByUser(aclUser.getUsername(), aclUser.getNickName());
        if (i==1){
            return new Result<>(2000,"修改备注成功!");
        }
        return new Result<>(4000,"修改备注失败!");
    }

    @Override
    public Result<IPage<AclUser>> updateUserPassword(String oldPassword, String newPassword) {
        //获取token中的用户名，并查取登录账号对象
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",TokenToAll.ToName());
        AclUser aclUser = aclUserDao.selectOne(wrapper);
        PasswordEncoder ps = new BCryptPasswordEncoder();
        //验证原密码是否与数据库中一致
        boolean matches = ps.matches(oldPassword, aclUser.getPassword());
        //修改为新密码
        if (matches){
            aclUser.setPassword(ps.encode(newPassword));
        }
        if (aclUserDao.updateUserPassword(aclUser.getId(), aclUser.getPassword())==1){
            return new Result<>(2000,"修改密码成功!");
        }
        return new Result<>(4000,"修改密码失败!");
    }

    @Override
    public Result<AclUser> uploadAvator(MultipartFile file) {
        OssUtils ossUtils = new OssUtils();
        String s =ossUtils.ossHou(file);
        if (s!=null){
            QueryWrapper<AclUser> wrapper=new QueryWrapper<>();
            wrapper.eq("username",TokenToAll.ToName());
            AclUser aclUser = aclUserDao.selectByName(TokenToAll.ToName());
            aclUser.setAvator(s);
            int update = aclUserDao.update(aclUser, wrapper);
            if(update==1){
                return new Result<>(2000,"修改头像成功!",aclUser);
            }
        }
        return new Result<>(4000,"修改头像失败!");
    }
}
